Imports System.IO
Imports System.Runtime.Serialization.Formatters.Binary

Public Class frmGrafo
    Public MatrizCaminos(,,) As Integer

    Public Sub New(ByVal matriz As Array)
        Me.InitializeComponent()
        Me.MatrizCaminos = matriz
        Me.MostrarMatrizCaminos()
    End Sub
    Private Sub frmGrafo_Paint(ByVal sender As System.Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles MyBase.Paint

    End Sub

    Private Sub TabPage1_Paint(ByVal sender As System.Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles TabPage1.Paint
        Dim i As Integer
        Dim puntos(,) As Integer
        puntos = Disposicion.CalcularPuntos(Me.TabPage1.Width, Me.TabPage1.Height)
        PuntosEstaticos.cargarPuntosDinamicos(puntos)
        Ciudad.crearCiudades(Me.MatrizCaminos.GetUpperBound(0))
        Camino.crearCaminos(Me.MatrizCaminos)

        e.Graphics.Clear(Color.White)

        For i = 0 To Camino.caminos.Count - 1
            Camino.caminos(i).dibujar(e.Graphics)
        Next

        For i = 0 To Camino.caminos.Count - 1
            Camino.caminos(i).dibujarDistancias(e.Graphics)
        Next

        For i = 0 To Ciudad.ciudades.Count - 1
            Ciudad.ciudades(i).dibujar(e.Graphics)
        Next
    End Sub

    Sub MostrarMatrizCaminos()
        Dim dtDistancias As New DataTable()
        Dim cantCiudades As Integer = Me.MatrizCaminos.GetUpperBound(0)
        Dim dr As DataRow

        'Genero los encabezados de las columnas
        For i As Integer = 1 To cantCiudades
            dtDistancias.Columns.Add(i)
        Next

        'Cargo el DataTable con los valores de los caminos
        For j As Integer = 0 To cantCiudades - 1
            dr = dtDistancias.NewRow()
            For k As Integer = 0 To cantCiudades - 1
                'Esto es para que solo se muestren los caminos que existan (Distantia <> 0)
                If Me.MatrizCaminos(j, k, 0) <> 0 Then
                    'Muestro el camino en el formato "Distancia(Aptitud)"
                    dr.Item(k) = Me.MatrizCaminos(j, k, 0).ToString() & "(" & Me.MatrizCaminos(j, k, 1).ToString() & ")" '
                End If
            Next
            dtDistancias.Rows.Add(dr)
        Next

        'Paso el datatable de los caminos al datagridview
        Me.dgvCiudades.DataSource = ""
        Me.dgvCiudades.DataSource = dtDistancias

        'Genero los encabezados de las filas
        For k As Integer = 0 To cantCiudades - 1
            Dim Encabezado As String = (k + 1).ToString()
            Me.dgvCiudades.Rows(k).HeaderCell.Value = Encabezado
        Next

        PuntosEstaticos.cargarPuntos()
        Ciudad.crearCiudades(MatrizCaminos.GetUpperBound(0))
        Camino.crearCaminos(MatrizCaminos)

        Me.Refresh()
    End Sub

    Private Sub btnAceptar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAceptar.Click
        Me.Close()
    End Sub

    Private Sub btnSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSave.Click
        Dim matriz As New Matrices()
        matriz.Caminos = Me.MatrizCaminos
        Dim myFileStream As Stream = File.Create("MatrizSalvada.bin")
        Dim serializer As New BinaryFormatter()
        serializer.Serialize(myFileStream, matriz)
        myFileStream.Close()
    End Sub
End Class